package com.scms.secondclass.service.impl;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.scms.common.utils.DateUtils;
import com.scms.common.utils.SecurityUtils;
import com.scms.secondclass.domain.Activity;
import com.scms.secondclass.domain.ActivityEnrollment;
import com.scms.secondclass.mapper.ActivityEnrollmentMapper;
import com.scms.secondclass.mapper.ActivityMapper;
import com.scms.secondclass.service.IActivityEnrollmentService;

/**
 * 活动报名Service业务层处理
 * 
 * @author scms
 */
@Service
public class ActivityEnrollmentServiceImpl implements IActivityEnrollmentService 
{
    @Autowired
    private ActivityEnrollmentMapper activityEnrollmentMapper;

    @Autowired
    private ActivityMapper activityMapper;

    /**
     * 查询活动报名
     * 
     * @param enrollmentId 活动报名ID
     * @return 活动报名
     */
    @Override
    public ActivityEnrollment selectActivityEnrollmentByEnrollmentId(Long enrollmentId)
    {
        return activityEnrollmentMapper.selectActivityEnrollmentByEnrollmentId(enrollmentId);
    }

    /**
     * 查询活动报名列表
     * 
     * @param activityEnrollment 活动报名
     * @return 活动报名
     */
    @Override
    public List<ActivityEnrollment> selectActivityEnrollmentList(ActivityEnrollment activityEnrollment)
    {
        return activityEnrollmentMapper.selectActivityEnrollmentList(activityEnrollment);
    }

    /**
     * 查询用户是否已报名活动
     * 
     * @param activityId 活动ID
     * @param userId 用户ID
     * @return 结果
     */
    @Override
    public boolean checkUserEnrolled(Long activityId, Long userId)
    {
        return activityEnrollmentMapper.checkUserEnrolled(activityId, userId) > 0;
    }

    /**
     * 查询活动报名人数
     * 
     * @param activityId 活动ID
     * @return 报名人数
     */
    @Override
    public int countActivityEnrollment(Long activityId)
    {
        return activityEnrollmentMapper.countActivityEnrollment(activityId);
    }

    /**
     * 新增活动报名
     * 
     * @param activityEnrollment 活动报名
     * @return 结果
     */
    @Override
    @Transactional
    public int insertActivityEnrollment(ActivityEnrollment activityEnrollment)
    {
        // 检查用户是否已报名该活动
        if (checkUserEnrolled(activityEnrollment.getActivityId(), activityEnrollment.getUserId()))
        {
            throw new RuntimeException("该用户已报名此活动");
        }

        // 检查活动是否存在且状态是否为已通过
        Activity activity = activityMapper.selectActivityByActivityId(activityEnrollment.getActivityId());
        if (activity == null)
        {
            throw new RuntimeException("活动不存在");
        }
        if (!"1".equals(activity.getStatus()))
        {
            throw new RuntimeException("活动未通过审核或已取消/结束");
        }

        // 检查当前时间是否在报名时间范围内
        Date now = DateUtils.getNowDate();
        if (now.before(activity.getSignupStartTime()) || now.after(activity.getSignupEndTime()))
        {
            throw new RuntimeException("不在报名时间范围内");
        }

        // 检查报名人数是否已满
        if (activity.getMaxParticipants() != null && activity.getMaxParticipants() > 0)
        {
            int count = countActivityEnrollment(activityEnrollment.getActivityId());
            if (count >= activity.getMaxParticipants())
            {
                throw new RuntimeException("报名人数已满");
            }
        }

        // 默认设置为待审核状态
        if (activityEnrollment.getStatus() == null)
        {
            activityEnrollment.setStatus("0");
        }
        activityEnrollment.setCreateTime(DateUtils.getNowDate());
        return activityEnrollmentMapper.insertActivityEnrollment(activityEnrollment);
    }

    /**
     * 修改活动报名
     * 
     * @param activityEnrollment 活动报名
     * @return 结果
     */
    @Override
    public int updateActivityEnrollment(ActivityEnrollment activityEnrollment)
    {
        activityEnrollment.setUpdateBy(SecurityUtils.getUsername());
        activityEnrollment.setUpdateTime(DateUtils.getNowDate());
        return activityEnrollmentMapper.updateActivityEnrollment(activityEnrollment);
    }

    /**
     * 更新活动报名状态
     * 
     * @param enrollmentId 报名ID
     * @param status 状态
     * @return 结果
     */
    @Override
    public int updateEnrollmentStatus(Long enrollmentId, String status)
    {
        ActivityEnrollment activityEnrollment = new ActivityEnrollment();
        activityEnrollment.setEnrollmentId(enrollmentId);
        activityEnrollment.setStatus(status);
        activityEnrollment.setUpdateBy(SecurityUtils.getUsername());
        activityEnrollment.setUpdateTime(DateUtils.getNowDate());
        return activityEnrollmentMapper.updateEnrollmentStatus(activityEnrollment);
    }

    /**
     * 更新活动报名签到状态
     * 
     * @param enrollmentId 报名ID
     * @return 结果
     */
    @Override
    public int updateEnrollmentSignIn(Long enrollmentId)
    {
        ActivityEnrollment activityEnrollment = new ActivityEnrollment();
        activityEnrollment.setEnrollmentId(enrollmentId);
        activityEnrollment.setSignTime(DateUtils.getNowDate());
        activityEnrollment.setUpdateBy(SecurityUtils.getUsername());
        activityEnrollment.setUpdateTime(DateUtils.getNowDate());
        return activityEnrollmentMapper.updateEnrollmentSignIn(activityEnrollment);
    }

    /**
     * 批量删除活动报名
     * 
     * @param enrollmentIds 需要删除的活动报名ID
     * @return 结果
     */
    @Override
    public int deleteActivityEnrollmentByEnrollmentIds(Long[] enrollmentIds)
    {
        return activityEnrollmentMapper.deleteActivityEnrollmentByEnrollmentIds(enrollmentIds);
    }

    /**
     * 删除活动报名信息
     * 
     * @param enrollmentId 活动报名ID
     * @return 结果
     */
    @Override
    public int deleteActivityEnrollmentByEnrollmentId(Long enrollmentId)
    {
        return activityEnrollmentMapper.deleteActivityEnrollmentByEnrollmentId(enrollmentId);
    }

    /**
     * 批量签到活动报名
     * 
     * @param enrollmentIds 需要签到的报名ID
     * @return 结果
     */
    @Override
    public int batchSignIn(Long[] enrollmentIds)
    {
        return activityEnrollmentMapper.batchSignIn(enrollmentIds, SecurityUtils.getUsername());
    }
} 